package com.example.chapter8broadcastrevicer;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    public static final String TAG = "TAG";
    public static final String ACTION = "action";
    MyReceiver receiver1 = new MyReceiver();
    MyReceiver2 receiver2 = new MyReceiver2();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("BroadCast");
        intentFilter.setPriority(1);
        registerReceiver(receiver1,intentFilter);
        intentFilter.setPriority(2);
        registerReceiver(receiver2,intentFilter);

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent("BroadCast");
                intent.putExtra(ACTION,"my action");
                sendOrderedBroadcast(intent,null);
                Log.e(TAG, "onClick: MainActivity");
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver1);
        unregisterReceiver(receiver2);
    }
}